package com.sakura.mall.coupon.homeSubjectSpu.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.coupon.homeSubjectSpu.domain.entity.HomeSubjectSpuEntity;
import com.sakura.mall.coupon.homeSubjectSpu.domain.request.AddHomeSubjectSpuRequest;
import com.sakura.mall.coupon.homeSubjectSpu.domain.request.UpdateHomeSubjectSpuRequest;
import com.sakura.mall.coupon.homeSubjectSpu.domain.request.HomeSubjectSpuPageRequest;
import com.sakura.mall.coupon.homeSubjectSpu.domain.vo.HomeSubjectSpuVo;
import com.sakura.mall.coupon.homeSubjectSpu.mapper.HomeSubjectSpuMapper;
import com.sakura.mall.coupon.homeSubjectSpu.service.HomeSubjectSpuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 专题商品 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class HomeSubjectSpuServiceImpl extends ServiceImpl<HomeSubjectSpuMapper, HomeSubjectSpuEntity> implements HomeSubjectSpuService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddHomeSubjectSpuRequest request) {
        // todo 其他校验逻辑
        HomeSubjectSpuEntity homeSubjectSpu = request.convert(HomeSubjectSpuEntity.class);
        this.save(homeSubjectSpu);
    }

    @Override
    public Page<HomeSubjectSpuVo> queryPage(HomeSubjectSpuPageRequest request) {
        IPage<HomeSubjectSpuEntity> qp = request.toPage();
        LambdaQueryWrapper<HomeSubjectSpuEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<HomeSubjectSpuVo> ret = qp.convert(entity -> entity.convert(HomeSubjectSpuVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateHomeSubjectSpuRequest request) {
        HomeSubjectSpuEntity entity = request.convert(HomeSubjectSpuEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

